package com.road.vast.system.role.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.road.vast.common.entity.PageResult;
import com.road.vast.common.entity.Result;
import com.road.vast.common.entity.ResultCode;
import com.road.vast.controller.BaseController;
import com.road.vast.pojo.system.PeRole;
import com.road.vast.system.role.service.PeRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 郭泽鹏
 * @since 2020-07-16
 */
@RestController
@RequestMapping("/role")
public class PeRoleController extends BaseController {
    @Resource
    private PeRoleService peRoleService;
    /**
     * 保存角色信息
     */
    @PostMapping("")
    public Result saveRole(@RequestBody PeRole peRole){
        peRole.setCompanyId(companyId);
        peRoleService.saveRole(peRole);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 更新角色信息
     */
    @PutMapping("/{id}")
    public Result updateRole(@RequestBody PeRole peRole,
                              @PathVariable(value = "id") String id){
        peRoleService.updateRole(peRole);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据id查询角色信息
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable(value = "id") String id){
        PeRole role = peRoleService.getById(id);
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(role);
        return result;
    }

    /**
     * 分页：查询所有
     */
    @GetMapping("")
    public Result getAll(Integer page, Integer pageSize){
        Page<PeRole> rolePage = peRoleService.getAll(page, pageSize);
        PageResult<PeRole> pageResult = new PageResult<>(rolePage.getTotal(), rolePage.getRecords());
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(pageResult);
        return result;
    }

    /**
     * 查询所有
     */
    @GetMapping("/list")
    public Result getList(){
        List<PeRole> roleList = peRoleService.getList();
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(roleList);
        return result;
    }

    /**
     * 查询角色已经分配到的权限
     */
    @GetMapping("/simple/{id}")
    public Result simple(@PathVariable(value = "id") String id){
        List<String> permIds = peRoleService.simple(id);
        PeRole role = peRoleService.getById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("permIds", permIds);
        map.put("role", role);
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(map);
        return result;
    }

    /**
     * 为角色分配权限
     */
    @PutMapping("/assignPrem")
    public Result assignPrem(@RequestBody Map<String, Object> map){
        //获取被分配的角色id
        String roleId = (String)map.get("id");
        //获取到权限的id列表
        List<String> permIds =  (List<String>)map.get("permIds");
        //调用service完成角色分配
        peRoleService.assignPrem(roleId, permIds);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据id删除
     */
    @DeleteMapping("/{id}")
    public Result deleteRole(@PathVariable(value = "id") String id){
        peRoleService.deleteRole(id);
        return new Result(ResultCode.SUCCESS);
    }
}

